package com.kajespero.webapp.controller.article;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.LinkedHashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.kajespero.model.ArticleBean;
import com.kajespero.model.UserBean;
import com.kajespero.service.article.impl.ArticleServiceImpl;
import com.kajespero.service.page.PageService;
import com.kajespero.service.page.impl.PageServiceImpl;
import com.kajespero.webapp.AttributesConstants;
import com.kajespero.webapp.controller.commons.AbstractController;

@Controller
@RequestMapping("/article")
public class ArticleController extends
		AbstractController<ArticleServiceImpl, ArticleBean> {
	
	private static final Log LOG = LogFactory.getLog(ArticleController.class);

	private static final PageService PAGE_SERVICE = new PageServiceImpl();

	@Override
	@RequestMapping(value = "/create_article")
	public ModelAndView create() {
		ModelAndView modelAndView = new ModelAndView("create_article");
		ArticleBean article = getService().createNewBean();
		modelAndView.addObject("article", article);
		modelAndView.addObject("pages", PAGE_SERVICE.findAll());
		return modelAndView;
	}

	@Override
	@RequestMapping(value = "/update_article")
	public ModelAndView update(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView("create_article");
		Long articleId = Long.decode(request.getParameter("id"));
		ArticleBean article = getService().find(articleId);
		modelAndView.addObject("article", article);
		modelAndView.addObject("pages", PAGE_SERVICE.findAll());
		return modelAndView;
	}

	@Override
	@RequestMapping(value = "/delete_article", method = RequestMethod.POST)
	public void delete(HttpServletRequest request) {
		Long articleId = Long.decode(request.getParameter("id"));
		getService().delete(articleId);
	}

	@Override
	@RequestMapping(value = "/save_article", method = RequestMethod.POST)
	public void saveOrUpdate(@ModelAttribute("article") ArticleBean instance,
			BindingResult result, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		ArticleBean oldArticle = getService().find(instance.getId());
		if (oldArticle != null) {
			instance.setKey(oldArticle.getKey());
			instance.setCreateBy(oldArticle.getCreateBy());
			instance.setModifyAt(new Date());
		} else {
			// It's a new article
			UserBean createBy = (UserBean) request.getSession().getAttribute(
					AttributesConstants.ADMINISTRATOR);
			instance.setCreateBy(createBy);
			instance.setCreateAt(new Date());
		}
		getService().saveOrUpdate(instance);
		String path = "/administration";
		String req = response.encodeRedirectURL("/index?"
				+ AttributesConstants.PATH + "=" + path);
		try {
			response.sendRedirect(req);
		} catch (IOException e) {
			LOG.error("IOException " + e.getMessage(), e);

		}
	}
	
	@RequestMapping(value = "/rest/getAllArticle", method= RequestMethod.POST)
	public ModelAndView getAllArticle(@RequestBody LinkedHashMap<String, Serializable> map){
		ModelAndView modelAndView = new ModelAndView();
		if(checkCredential(map)){
			modelAndView.addObject("articles", getService().findAll());
		}
		return modelAndView;
	}
	
	@RequestMapping(value="/rest/saveArticle", method=RequestMethod.POST)
	public void saveOrUpdate(@RequestBody LinkedHashMap<String, Serializable> map){
		if(checkCredential(map)){
			// TODO A completer 
		}
	}
}
